<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-row style="height:60px">
            <el-button type="primary" icon="el-icon-plus" size="small" @click="addRole">新增角色</el-button>
          </el-row>
          <el-table
            border
            :data="list"
            style="width: 100%"
          >
            <el-table-column
              type="index"
              align="center"
              label="序号"
              width="120"
            />
            <el-table-column
              prop="name"
              label="名称"
              align="center"
              width="240"
            />
            <el-table-column
              prop="description"
              align="center"
              label="描述"
            />
            <el-table-column
              label="操作"
            >
              <template slot-scope="{row}">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
              </template>

            </el-table-column>
          </el-table>
          <el-row type="flex" justify="center" align="middle" style="height: 60px">
            <el-pagination
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.pagesize"
              @current-change="handleCurrentChange"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            :closable="false"
            type="info"
            show-icon
          />
          <el-form ref="form" label-width="120px" style="margin-top: 50px;">
            <el-form-item label="企业名称">
              <el-input v-model="formData.name" disabled style="width: 400px;" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width: 400px;" />
            </el-form-item>
            <el-form-item label="电话">
              <el-input v-model="formData.companyPhone" disabled style="width: 400px;" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width: 400px;" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px;" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 编辑对话框 -->
      <el-dialog
        :title="formRole.id ? '编辑角色':'新增角色'"
        :visible.sync="showDialog"
        width="40%"
        @close="btnCancel"
      >
        <el-form ref="formRoleRef" :model="formRole" :rules="formRules" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formRole.name" style="width: 400px;" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="formRole.description" style="width: 400px;" />
          </el-form-item>
        </el-form>
        <el-row type="flex" justify="center">
          <el-button @click="btnCancel">取消</el-button>
          <el-button type="primary" @click="btnOk">确定</el-button>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/setting'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  props: {},
  data() {
    return {
      activeName: 'first',
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formData: {},
      formRole: {
        name: '',
        description: ''
      },
      showDialog: false,
      formRules: {
        name: [{ required: true, message: '角色名不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  mounted() {},
  methods: {
    addRole() {
      this.showDialog = true
    },
    async editRole(id) {
      this.formRole = await getRoleDetail(id)
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.formRole = this.$options.data().formRole
      this.$refs.formRoleRef.resetFields()
    },
    async btnOk() {
      try {
        await this.$refs.formRoleRef.validate()
        let message = ''
        // 如果有 id 说明是编辑
        if (this.formRole.id) {
          await updateRole(this.formRole)
          message = '编辑成功'
          this.showDialog = false
        } else {
          // 否则是 新增
          await addRole(this.formRole)
          message = '新增成功'
          this.showDialog = false
        }
        this.getRoleList()
        this.$message({
          message,
          type: 'success',
          showClose: true,
          duration: 3000
        })
      } catch (err) {
        // 验证失败
        return Promise.reject(err)
      }
    },
    handleCurrentChange(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    async deleteRole(id) {
      // 删除
      this.$confirm('确认删除该角色吗', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'success'
      })
        .then(async action => {
          await deleteRole(id)
          this.$message({
            message: '删除成功',
            type: 'success',
            showClose: true,
            duration: 3000
          })
          this.getRoleList()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
